"use strict";
// Class definition

var myLaunchList = function () {

    var datatable;
    var eventHandler = function () {
        $('.k_selectpicker').selectpicker();

        $(document).on('click', '.btn-search', doSearch);
        $(document).on('click', '.btn-refresh', doRefresh);
        $(document).on('click', '.status-btn', doSearchStatus);
    };
    // Private functions
    // basic demo
    var initMyLaunchList = function () {
        datatable = $('#my-launch').KDatatable({
            // datasource definition
            data: {
                type: 'remote',
                source: {
                    read: {
                        url: 'json/my-launch-list.json',
                        method: 'GET'
                        /**  格式化数据（默认）
                         map: function(raw) {
							// sample data mapping
							var dataSet = raw;
							if (typeof raw.data !== 'undefined') {
								dataSet = raw.data;
							}
							return dataSet;
						},
                         */
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },

            // layout definition
            layout: {
                scroll: false,
                footer: false
            },

            // column sorting
            sortable: true,

            pagination: true,

            translate: {
                records: {
                    processing: '数据加载中...',
                    noRecords: '未匹配到对应数据'
                },
                toolbar: {
                    pagination: {
                        items: {
                            default: {
                                first: '第一页',
                                prev: '前一页',
                                next: '下一页',
                                last: '最后一页',
                                more: '更多',
                                input: 'Page number',
                                select: '选择页面显示数'
                            },
                            info: '显示 {{start}} - {{end}} 的 {{total}}'
                        }
                    }
                }
            },

            search: {
                input: $('#generalSearch')
            },

            // columns definition
            columns: [
                {
                    field: 'type',
                    title: '类型',
                    sortable: false
                },
                {
                    field: 'abstract',
                    title: '待办摘要',
                    textAlign: 'left',
                    sortable: false,
                    template: function (row, index, el) {
                        return '\
                        <span class="text-primary">进入事由：' + row[this.field].reasons + '</span><br>\
                        <span class="text-primary">申请人组织/部门：' + row[this.field].org + '</span><br>\
                        <span class="text-primary">申请人：' + row[this.field].user + '</span><br>\
                        <span class="text-primary">最后审批人：' + row[this.field].approver + '</span>\
                        ';
                    }
                },
                {
                    field: 'started_time',
                    title: '发起日期',
                    sortable: true
                },
                {
                    field: 'final_treatment_time',
                    title: '最后处理时间',
                    sortable: true
                },
                {
                    field: 'status',
                    title: '状态',
                    sortable: true,
                    template: function (row, index, el) {
                        var mark = '';
                        if (row[this.field] === 0) {
                            mark = '待<span class="text-primary">' + row['next_handler'] + '</span>处理'
                        }
                        if (row[this.field] === 1) {
                            mark = '<span class="text-success">审核通过</span>'
                        }
                        if (row[this.field] === 2) {
                            mark = '<span class="text-danger">被' + row['next_handler'] + '驳回</span>'
                        }
                        if (row[this.field] === 3) {
                            mark = '<span class="text-secondary">已作废</span>'
                        }
                        return mark;
                    }
                },
                {
                    field: 'Actions',
                    title: '操作',
                    width: 180,
                    overflow: 'visible',
                    sortable: false,
                    textAlign: 'left',
                    template: function (row) {
                        var btnHtml = '';
                        if (row['status'] === 2) {
                            btnHtml = '<a href="#" class="text-success">编辑</a>'
                        } else {
                            btnHtml = '<a href="#" class="text-primary">处理</a>'
                        }
                        return btnHtml
                    }
                }
            ]
        });
    };

    var doSearch = function () {
        var query = datatable.getDataSourceQuery();
        query.type = $('select.type').val();
        query.started_time = $('#started_time').val();
        query.final_treatment_time = $('#final_treatment_time').val();
        datatable.setDataSourceQuery(query);
        datatable.search();
    };

    var doRefresh = function () {
        var query = datatable.getDataSourceQuery();
        var dateScope = moment().format('YYYY-MM-DD') + ' ~ ' + moment().format('YYYY-MM-DD');
        $('select.type').selectpicker('val', '');
        $('#started_time').val(dateScope);
        $('#final_treatment_time').val(dateScope);
        query.type = '';
        query.started_time = dateScope;
        query.final_treatment_time = dateScope;
        datatable.setDataSourceQuery(query);
        datatable.search();
    };

    var doSearchStatus = function () {
        var query = datatable.getDataSourceQuery();
        query.status = $(this).data('status');
        datatable.setDataSourceQuery(query);
        datatable.search();

        $(this).addClass('active').siblings().removeClass('active')
    };

    var initStartingTime = function () {
        $('#started_time').daterangepicker();

        $('#final_treatment_time').daterangepicker();
    };

    return {
        // public functions
        init: function () {
            eventHandler();
            initStartingTime();
            initMyLaunchList();
        }
    };
}();

jQuery(document).ready(function () {
    myLaunchList.init();
});
